package tongwentongshu.com.app.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.BindView;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.adapter.NullAdapter;
import tongwentongshu.com.app.bean.LoginEvent;
import tongwentongshu.com.app.bean.PayEvent;
import tongwentongshu.com.app.contract.RealNameAuthenticationContract;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.presenter.ReadingDetailsPresenter;
import tongwentongshu.com.app.presenter.RealNameAuthenticationPresenter;
import tongwentongshu.com.app.utils.ToastUtil;

/**
 * Created by XiongBin on 2017/7/3.
 */

public class RealNameAuthenticationActivity extends BaseActivity implements RealNameAuthenticationContract.View, View.OnClickListener {

    @BindView(R.id.rv_list)
    RecyclerView mRecyclerView;
    @BindView(R.id.iv_real_one)
    ImageView one;
    @BindView(R.id.iv_real_two)
    ImageView two;
    @BindView(R.id.iv_real_three)
    ImageView three;

    private NullAdapter authenticationAdapter;
    private RealNameAuthenticationPresenter realNameAuthenticationPresenter;
    private Button next;
    private View name, deposit, complete;

    private int pager_type = 1;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_real_name_authentication;
    }

    @Override
    protected int getTitleId() {
        return R.string.authentication;
    }

    @Override
    protected void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        authenticationAdapter = new NullAdapter();
        authenticationAdapter.openLoadAnimation();
        mRecyclerView.setAdapter(authenticationAdapter);
        if (Cache.getUser().getData().getUser_status().equals("1")) {
            pager_type = 2;
            getDepositView();
            authenticationAdapter.addHeaderView(deposit);
            one.setImageResource(R.drawable.one_small);
            two.setImageResource(R.drawable.two_big);
            three.setImageResource(R.drawable.three_small);
            pager_type = 2;
        } else {
            pager_type = 1;
            getNameView();
            authenticationAdapter.addHeaderView(name);
        }

    }

    EditText nameText;
    EditText idText;


    @Override
    protected void initData() {
        realNameAuthenticationPresenter = new RealNameAuthenticationPresenter(this);
    }


    public View getNameView() {
        name = getLayoutInflater().inflate(R.layout.authentication_name, (ViewGroup) mRecyclerView.getParent(), false);

        nameText = (EditText) name.findViewById(R.id.et_name);
        idText = (EditText) name.findViewById(R.id.et_id);
        next = (Button) name.findViewById(R.id.btn_next);
        next.setOnClickListener(this);
        return name;
    }

    public View getDepositView() {
        deposit = getLayoutInflater().inflate(R.layout.authentication_deposit, (ViewGroup) mRecyclerView.getParent(), false);
        next = (Button) deposit.findViewById(R.id.btn_next);
        ((Button) deposit.findViewById(R.id.btn_share)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView depositText = (TextView) deposit.findViewById(R.id.tv_d);
        depositText.setText(Cache.getUser().getData().getDeposit() + "元");
        next.setOnClickListener(this);
        return deposit;
    }

    public View getCompleteView() {
        complete = getLayoutInflater().inflate(R.layout.authentication_complete, (ViewGroup) mRecyclerView.getParent(), false);
        next = (Button) complete.findViewById(R.id.btn_next);
        next.setOnClickListener(this);
        return complete;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_next:
                if (pager_type == 1) {
                    realNameAuthenticationPresenter.verificationID(mContext);
                } else if (pager_type == 2) {
                    realNameAuthenticationPresenter.payDeposit(mContext, "1");
                } else {
                    finish();
                }

                break;
            default:
                break;
        }
    }

    //支付回调
    public void onEventMainThread(PayEvent result) {
        switch (result.errCode) {
            case 0:
                Cache.getUser().getData().setMoney("1");
                getCompleteView();
                authenticationAdapter.addHeaderView(complete);
                authenticationAdapter.removeHeaderView(deposit);
                one.setImageResource(R.drawable.one_small);
                two.setImageResource(R.drawable.two_small);
                three.setImageResource(R.drawable.three_big);
                pager_type = 3;
                break;
            case -1:
                ToastUtil.show(mContext, "支付失败");
                break;
            case -2:
                ToastUtil.show(mContext, "支付中断");
                break;
            default:
                break;
        }
    }

    @Override
    public String getName() {
        return nameText.getText().toString().trim();
    }

    @Override
    public String getID() {
        return idText.getText().toString().trim();
    }

    @Override
    public void verificationAdopt() {
        Cache.getUser().getData().setUser_status("1");
        getDepositView();
        authenticationAdapter.addHeaderView(deposit);
        authenticationAdapter.removeHeaderView(name);
        one.setImageResource(R.drawable.one_small);
        two.setImageResource(R.drawable.two_big);
        three.setImageResource(R.drawable.three_small);
        pager_type = 2;
    }
}
